package com.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dao.dao.IGoodsDao;
import com.dao.dao.IOrderDao;
import com.dao.dao.IOrderGoodsDao;
import com.dao.dao.IUserDao;
import com.dao.entity.Goods;
import com.dao.entity.Order;
import com.dao.entity.OrderGoods;
import com.dao.redis.IRedislLoginVerifyDao;
import com.dao.redis.redisEntity.LoginVerify;

/**
 * 没有被拦截的测试控制器
 * 
 * @author 尚业
 *
 */
@Controller
public class HelloReaderController {
	private static Logger logger = Logger.getLogger(HelloReaderController.class);
	
	
	@Autowired
	IUserDao userDao;
	@Autowired
	IOrderDao orderDao;
	@Autowired
	IOrderGoodsDao orderGoodsDao;
	@Autowired
	IRedislLoginVerifyDao  redislLoginVerifyDao;
	@Autowired
	IGoodsDao goodsDao;
	/**
	 * 1.获取事物控制器
	 */
	@Autowired
	DataSourceTransactionManager transactionManager;
	
	@RequestMapping(value = "/hello")
	public ModelAndView sayHello(HttpSession session, HttpServletResponse response,HttpServletRequest requests) {
		ModelAndView mv = new ModelAndView();
		mv.addObject("message", session.getId());
		mv.setViewName("helloReader");
		Order order=new Order();
		order.setSellername("aaa");
		order.setCreatetime(new Date());
		order.setState(0);
		order.setUsername("bbb");
		Goods goods=new Goods();
		goods.setGoodsid(1);
		goods.setGoodsdescribe("asdfsadf");
		goods.setNum(54);
		List<Goods> goodsList =new ArrayList<Goods>();
		goodsList.add(goods);
		if(addOrder(order,goodsList )){
			logger.warn("操作成功");
		}else{
			logger.warn("操作失败");
		}
		
		return mv;
	}

	private boolean addOrder(Order order,List<Goods> goodsList ){
		boolean flg=false;
		//2.获取事物定义
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		//3.设置事务隔离级别，开启新事务
	    def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
	    //4.获得事务状态
	    TransactionStatus status = transactionManager.getTransaction(def);
	    try{
	    orderDao.insert(order);
	    int newOrderID=orderDao.selectMaxOrderId();
		for (int i = 0; i < goodsList.size(); i++) {
			OrderGoods temp = new OrderGoods();
			temp.setId(1);
			temp.setOrderid(newOrderID);
			temp.setGoodsid(goodsList.get(i).getGoodsid());
			temp.setGoodsname(goodsList.get(i).getGoodsname());
			temp.setGoodssort(goodsList.get(i).getGoodssort());
			temp.setNum(goodsList.get(i).getNum());//注意此处数量的区分，
			temp.setPrice(goodsList.get(i).getPrice());
			temp.setUsername(goodsList.get(i).getUsername());
			orderGoodsDao.insert(temp);
		}
		transactionManager.commit(status);
		flg=true;
		
	    }catch(Exception e){
	    	transactionManager.rollback(status);
	    	e.printStackTrace();
	    }
		return flg;
	}
	
	@RequestMapping(value = "/get")
	public void getData() {
		
		LoginVerify verify=redislLoginVerifyDao.get("1111");
		if(verify!=null){
		System.out.println(verify.getToken());
		System.out.println(verify.getTimestamp());
		System.out.println(verify.getUserName());
		}else{
			System.out.println("空数据");
		}
	}
}
